package com.dm.cloud.images.mode;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description ="应用节点的基本信息")
@Data
public class AppNodeInfoModel {

    @Schema(description = "节点ID")
    private String nodeId;

    @Schema(description = "节点名称")
    private String nodeName;

    @Schema(description = "节点地址")
    private String nodeAddress;

    @Schema(description = "容器总量")
    private int containerSize=0;

    @Schema(description = "节点状态:0是异常 1是正常")
    private int nodeStatus;

    @Schema(description = "容器状态,目前支持5种：created（已创建），running（运行中），paused（暂停），exited（停止），dead（死亡）")
    private LinkedHashMap<String,Integer> containerStatus=new LinkedHashMap<String,Integer>(){{
        put("created",0);
        put("running",0);
        put("paused",0);
        put("exited",0);
        put("dead",0);
    }};

    @Schema(description = "容器基本信息")
    private List<ContainerInfo> containerInfos;

    public void addContainerInfo(ContainerInfo containerInfo){
        if(this.containerInfos==null){
            this.containerInfos=new ArrayList<>();
        }
        this.containerInfos.add(containerInfo);
    }

    /**
     * 添加容器总量
     * @param i
     */
    public void addContainerSize(int i){
        this.containerSize+=i;
    }

    /**
     * 添加容器状态统计
     * @param status
     * @param i
     */
    public void addStatus(String status, int i) {
        if(containerStatus.containsKey(status)){
            int x=containerStatus.get(status);
            containerStatus.put(status,x+i);
        }else{
            containerStatus.put(status,i);
        }
    }

    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description ="节点中的容器基本信息")
    @Data
    public static class ContainerInfo{
        @Schema(description = "容器ID")
        private String id;
        @Schema(description = "容器名称")
        private String[] names;
        @Schema(description = "容器关联的镜像")
        private String image;
        @Schema(description = "执行命令")
        private String command;
        @Schema(description = "容器状态,目前支持5种：created（已创建），running（运行中），paused（暂停），exited（停止），dead（死亡）")
        private String status;
    }

}
